package cn.exrick.xboot.modules.system.serviceimpl;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.modules.AccessControl.entity.AccessControlUser;
import cn.exrick.xboot.modules.AccessControl.service.IAccessControlUserService;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.system.dao.mapper.SystemMapper;
import cn.exrick.xboot.modules.system.entity.System;
import cn.exrick.xboot.modules.system.service.ISystemService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 子系统的快捷入口信息接口实现
 * @author Exrick
 */
@Slf4j
@Service
@Transactional
public class ISystemServiceImpl extends ServiceImpl<SystemMapper, System> implements ISystemService {

    @Autowired
    private SystemMapper systemMapper;
    @Autowired
    private IAccessControlUserService iAccessControlUserService;

    /**
     * 20190724
     * njp
     * 新增快捷入口的条件查询(使用map作为条件的载体)
     * map 为参数
     */
    @Override
    public IPage<System> selectSystemPageVoMap(Page page, @Param("map") Map<String,Object> map){
        return systemMapper.selectSystemPageVoMap(page,map);
    }

    /**
     * 20190729
     * njp
     * 新增快捷入口的portal查询
     * map 为参数
     */
    @Override
    public List<System> selectSystemPortal(@Param("user") User  user){

        return systemMapper.selectSystemPortal(user);
    }

    /**
     * 20191024
     * njp
     * 新增APP快捷入口的查询
     */
    @Override
    public List<System> selectSystemAPP(){
        return systemMapper.selectSystemAPP();
    }


    /**
     * 20200611
     * njp
     * 在acu表中查询该系统限制名单中是否存在当前用户
     */
    @Override
    public List<System> checkSysByAcu(List<System> sysList,User u){
        log.info(u.toString());
        AccessControlUser acu=null;
        List<System> newSysList=new ArrayList<>();
        for (System system : sysList) {
            acu=new AccessControlUser(u.getUsername(),system.getId());
            //1.判断当前系统是否开启网络访问限制
            /*CommonConstant*/
            if (CommonConstant.ACUSTART==system.getAcStatus()) {
                //2.在acu表中查询该系统限制名单中是否存在当前用户
                List<AccessControlUser> acuBySysAndUser = iAccessControlUserService.findAcuBySysAndUser(acu);
                if(acuBySysAndUser.size()>0){
                    //将当前用户当前系统的单点地址换成内网地址
                    system.setUrl(system.getIntranetUrl());
                }
            }
            newSysList.add(system);
        }
        return newSysList;
    }
}